<?php

class Tpl{
	static public function select($name, $option, $value = 0){
		$html = '<select name="'.$name.'">';
		if(!is_array($option)){
			$option = explode(',', $option);
		}
		foreach($option as $key => $val){
			$html.= '<option value="'.$key.'"'.($value == $key ? ' selected="selected"' : '').'>'.$val.'</option>';
		}
		$html.= '</select>';
		
		return $html;
	}
	
	static public function mpage($totalnum, $page, $limit, $url = ''){
		if(!$url){
			$url = $_SERVER['SCRIPT_FILENAME'].($_SERVER['QUERY_STRING'] ? '?'.$_SERVER['QUERY_STRING'] : '');
		}
		$delimeter = strpos($url, '?') !== false ? '&' : '?';
		
		$maxpage = ceil($totalnum / $limit);
		if($maxpage <= 1){
			return '';
		}
		
		$page = min($maxpage, max(1, intval($page)));
		
		$html = '<div class="mpage">';
		$html.= '<a href="'.$url.$delimeter.'page=1'.'">'."首页".'</a>';

		$html.= '<a href="'.$url.$delimeter.'page='.max(1,$page - 1).'">'."上一页".'</a>';
		for($i = max(1, $page - 5 - max(0,$page + 5 - $maxpage)); $i <= min($maxpage, $page + 5 + max(0, 6 - $page)); $i++){
			if($i == $page){
				$html.= '<a href="###" class="current">'.$i.'</a>';
			}else{
				$html.= '<a href="'.$url.$delimeter.'page='.$i.'">'.$i.'</a>';
			}
		}
		$html.= '<a href="'.$url.$delimeter.'page='.min($maxpage,$page + 1).'">'."下一页".'</a>';
		$html.= '<a href="'.$url.$delimeter.'page='.$maxpage.'">'."尾页".'</a>';
		$html.= '</div>';
		
		
		return $html;
	}
	
	static public function checkbox($name, $tips, $value){
		return '<input type="checkbox" id="'.$name.'" name="'.$name.'"'.($value ? ' checked="checked"' : '').' /><span class="checkbox">'.$tips.'</span>';
	}
}

?>